package com.qf.InternetProgram;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class TCPSocketHostBySend {
	public static void main(String[] args) {
		sendMessageBySocket();
		//程序A
	}

	public static void sendMessageBySocket() {
		
		
		try {
			
			
			//3.请求与B建立连接
			Socket socket = new Socket("192.168.51.58",8090);
			
			System.out.println("正在与TCPSocketHostByGet类连接...");
			
			//可以发消息
			OutputStream os = socket.getOutputStream();
			//可以接收消息
			InputStream is = socket.getInputStream();
			
			
			//字节流转化为字符流
			//BufferedReader是什么？？
			
			//将字节输入流转换成字符输入流
			InputStreamReader charStream = new InputStreamReader(is);
			
			BufferedReader br = new BufferedReader(charStream);
			
			PrintWriter pw = new PrintWriter(os);
			
			Scanner sc = new Scanner(System.in);
			
			
			
			Date d =new Date();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			String date = sdf.format(d);
			
			//4.程序A控制台扫描一句话发送给B
			System.out.println("【客户端】请输入一句话给服务器：");
			String s = sc.nextLine();
			
			pw.println(s);
			
			pw.println(date);
			
			pw.flush();
			
			
			//7.程序A接收程序B的回复
			s= br.readLine();
			System.out.println("回复B程序给A程序回复的消息："+s);
			
			
			
		}catch(IOException e) {
			
			System.out.println("IO流数据交换时发生异常...");
			
		}
		
		
	}
}
